package com.dai.spec.gxk.controller;

import com.dai.spec.common.annotation.Log;
import com.dai.spec.common.core.controller.BaseController;
import com.dai.spec.common.core.domain.AjaxResult;
import com.dai.spec.common.core.page.TableDataInfo;
import com.dai.spec.common.enums.*;
import com.dai.spec.common.exception.base.BaseException;
import com.dai.spec.common.utils.poi.ExcelUtil;
import com.dai.spec.gxk.domain.StudentCourse;
import com.dai.spec.gxk.request.PageRequest;
import com.dai.spec.gxk.service.SelectedCourseService;
import com.dai.spec.gxk.vo.SelectedCourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
@SuppressWarnings("all")
@RestController
@RequestMapping("/gxk/selectCourse")
@Api(tags = "选课管理")
public class SelectedCourseController extends BaseController {
    private SelectedCourseService selectedCourseService;

    public SelectedCourseController(SelectedCourseService selectedCourseService) {
        this.selectedCourseService = selectedCourseService;
    }

    /**
     * 选课
     */
    @ApiOperation("选课")
    @PreAuthorize("@ss.hasPermi('gxk:selectCourse:select')")
    @Log(title = "选课管理", businessType = BusinessType.INSERT)
    @PostMapping("/{id}")
    public AjaxResult add(@PathVariable("id") Long courseId) {
        StudentCourse studentCourse = new StudentCourse();
        this.checkCourseId(courseId);
        return toAjax(selectedCourseService.saveSelectedCourse(courseId));
    }

    /**
     * 退课
     */
    //
    @ApiOperation("退课")
    @PreAuthorize("@ss.hasPermi('gxk:selectCourse:drop')")
    @Log(title = "选课管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult drop(@PathVariable("id") Long courseId) {
        StudentCourse studentCourse = new StudentCourse();
        this.checkCourseId(courseId);
        return toAjax(selectedCourseService.dropSelectedCourse(courseId));
    }

    /**
     * 查询已选课程列表
     * @param
     * @return
     */
    @ApiOperation("查询已选课程列表")
    @PreAuthorize("@ss.hasPermi('gxk:selectCourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageRequest pageRequest){
//        startPage();
        List<SelectedCourseVO> list = selectedCourseService.querySelectedCourseList(pageRequest);
        return getDataTable(list);
    }

    /**
     * 导出已选课程信息管理列表
     */
    @ApiOperation("导出已选课程信息列表")
    @PreAuthorize("@ss.hasPermi('gxk:selectCourse:export')")
    @Log(title = "课程信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageRequest pageRequest) {

        List<SelectedCourseVO> list = selectedCourseService.querySelectedCourseList(pageRequest);
        List<SelectedCourseVO> selectedCourseVOList = list.stream().map(item -> {
            SelectedCourseVO selectedCourseVO = coverCourse(item);
            return selectedCourseVO;
        }).collect(Collectors.toList());
        ExcelUtil<SelectedCourseVO> util = new ExcelUtil<>(SelectedCourseVO.class);
        util.exportExcel(response, selectedCourseVOList, "已选课程信息管理数据");
    }

    private static SelectedCourseVO coverCourse(SelectedCourseVO item) {
        String department = item.getDepartment();
        item.setDepartment(DepartmentType.getValueByCode(department));

        String courseCycle = item.getCourseCycle();
        item.setCourseCycle(CourseCycleType.getValueByCode(courseCycle));

        String type = item.getType();
        item.setType(CourseType.getValueByCode(type));

        String status = item.getStatus();
        item.setStatus(CourseStatus.getValueByCode(status));

        String deliveryType = item.getDeliveryType();
        item.setDeliveryType(DeliveryType.getValueByCode(deliveryType));

        item.setSelectStatus(SelectStatus.getValueByCode(item.getSelectStatus()));
        return item;
    }


    private static void checkCourseId(Long courseId) {
        if (null == courseId){
            throw new BaseException("当前课程不存在!");
        }
    }
}
